/*
 * @(#)ProductServiceImpl.java 2011-11-08
 *
 * Copyright 2011 北龙中网（北京）科技有限责任公司. All rights reserved.
 */
package cn.shangjia.shop.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.shangjia.common.dao.support.Page;
import cn.shangjia.common.domain.ProductOnline;
import cn.shangjia.common.exceptions.Logger;
import cn.shangjia.common.util.StringUtils;
import cn.shangjia.shop.dao.ProductOnlineDao;
import cn.shangjia.shop.exception.NotFoundException;
import cn.shangjia.shop.log.ShopLoggerFactory;
import cn.shangjia.shop.service.ProductService;

/**
 * 供应产品服务实现.
 *
 * @author <a href="mailto:wangxin@knet.cn">wangxin</a>
 * @version 1.0, 2011-11-08 18:13:36
 * @since 1.0
 */
@Service
public class ProductServiceImpl implements ProductService {
	
    @SuppressWarnings("unused")
	private static final Logger logger = ShopLoggerFactory.getLogger(ProductServiceImpl.class);
	
	@Resource
	private ProductOnlineDao productOnlineDao;

	@Override
	public Page<ProductOnline> findProductNewListByUserId(String userId, int pageNo,int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		Page<ProductOnline> pageTemp = new Page<ProductOnline>();
		pageTemp.setAutoCount(false);
		pageTemp.setPageNo(pageNo);
		pageTemp.setPageSize(pageSize);
		return productOnlineDao.selectListPage(pageTemp, params);
	}
	
	@Override
	public Page<ProductOnline> findProductRecommendListByUserId(String userId, int pageNo,int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		Page<ProductOnline> pageTemp = new Page<ProductOnline>();
		pageTemp.setAutoCount(false);
		pageTemp.setPageNo(pageNo);
		pageTemp.setPageSize(pageSize);
		pageTemp.setOrderBy("visit_num");
		pageTemp.setOrder("desc");
		return productOnlineDao.selectListPage(pageTemp, params);
	}

	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.service.ProductService#findProductPage(java.lang.String, java.lang.String, int, int)
	 */
	@Override
	public Page<ProductOnline> findProductPage(String userId, String dir, int pageNo,int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		if (!StringUtils.isEmpty(dir))
			params.put("dir", dir);
		
		Page<ProductOnline> page = new Page<ProductOnline>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page.setAutoCount(false);
		page.setTotalCount(productOnlineDao.totalCount(params));
		
		return productOnlineDao.selectListPage(page, params);
	}

	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.service.ProductOnlineService#getProductById(java.lang.String)
	 */
	@Override
	public ProductOnline getProductById(String id) {
		if (StringUtils.isEmpty(id))
			throw new NotFoundException();
		return productOnlineDao.read(id);
	}
}
